{{- define "title" }}{{ .Title }} - {{ .Site.Title }}{{ end -}}

{{- define "content" -}}
    {{- .Page.Scratch.SetInMap "this" "tablesort" true -}}
    {{- $params := .Scratch.Get "params" -}}
    {{- $toc := $params.toc -}}
    {{- if eq $toc true -}}
    {{- $toc = .Site.Params.page.toc | default dict -}}
    {{- else if eq $toc false -}}
    {{- $toc = dict "enable" false -}}
    {{- end -}}
    
    
    {{- /* Auto TOC */ -}}
    {{- if $toc.enable | and (eq $toc.keepStatic false) -}}
    <div class="toc print:!tw-hidden" id="toc-auto">
        <h2 class="toc-title">{{ T "contents" }}</h2>
        <div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}" id="toc-content-auto">
            {{- dict "Content" .TableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
        </div>
    </div>
    {{- end -}}
    
    {{- /* TOC Style */ -}}
    {{- if eq $toc.enable true -}}
        {{- if eq $toc.keepStatic true -}}
            <script>document.getElementsByTagName("main")[0].setAttribute("autoTOC", "false")</script>
        {{- else -}}
            <script>document.getElementsByTagName("main")[0].setAttribute("autoTOC", "true")</script>
        {{- end -}}
    {{- else if eq $toc.enable false -}}
        <script>document.getElementsByTagName("main")[0].setAttribute("autoTOC", "false")</script>
    {{- end -}}

    <article class="page single print:!tw-w-full print:!tw-max-w-none print:!tw-m-0 print:!tw-p-0">
        {{- /* Title */ -}}
        <h1 class="single-title">{{ .Title }}</h1>

        {{- /* Subtitle */ -}}
        {{- with $params.subtitle -}}
            <h2 class="single-subtitle">{{ . }}</h2>
        {{- end -}}

        {{- /* Meta */ -}}
        <div class="post-meta">
            <div class="post-meta-line">
                <span class="post-author">
                    {{- partial "meta/author.html" . }}
                </span>
                {{- with .Params.categories | or .Params.series -}}
                    &nbsp;<span class="post-category">
                        {{- T "includedIn" | safeHTML -}}
                    </span>
                {{- end -}}
                
                {{- $categories := slice -}}
                {{- range .Params.categories -}}
                    {{- $category := partialCached "function/path.html" . . | printf "/categories/%v" | $.Site.GetPage -}}
                    {{- $icon := partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "folder") -}}
                    {{- $categories = $categories | append (printf `<a href="%v">%v%v</a>` $category.RelPermalink $icon $category.Title) -}}
                {{- end -}}
                {{- with delimit $categories "&nbsp;" -}}
                    &nbsp;<span class="post-category">
                        {{- dict "Categories" . "Count" (len $categories) | T "includedInCategories" | safeHTML -}}
                    </span>
                {{- end -}}
                
                {{- with (and (.Params.categories) (.Params.series)) -}}
                    &nbsp;<span class="post-category">
                        {{- T "includedInAnd" | safeHTML -}}
                    </span>
                {{- end -}}

                {{- $series := slice -}}
                {{- range .Params.series -}}
                    {{- $singleSeries := partialCached "function/path.html" . . | printf "/series/%v" | $.Site.GetPage -}}
                    {{- $icon := partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "list-alt") -}}
                    {{- $series = $series | append (printf `<a href="%v">%v%v</a>` $singleSeries.RelPermalink $icon $singleSeries.Title) -}}
                {{- end -}}
                {{- with delimit $series "&nbsp;" -}}
                    &nbsp;<span class="post-series">
                        {{- dict "Series" . | T "includedInSeries" | safeHTML -}}
                    </span>
                {{- end -}}
            </div>
            <div class="post-meta-line">
                {{- with .Site.Params.dateformat | default "2006-01-02" | .PublishDate.Format -}}
                    {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "calendar-alt") }}&nbsp;<time datetime="{{ . }}">{{ . }}</time>&nbsp;
                {{- end -}}
                {{- if $params.enableLastMod | default true -}}
                    {{- with .Site.Params.dateformat | default "2006-01-02" | .Lastmod.Format -}}
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "edit") }}&nbsp;<time datetime="{{ . }}">{{ . }}</time>&nbsp;
                    {{- end -}}
                {{- end -}}
                {{- if $params.enableWordCount | default true -}}
                    {{ partial "plugin/fontawesome.html" (dict "Style" "solid" "Icon" "pencil-alt") }}&nbsp;{{ T "wordCount" .WordCount }}&nbsp;
                {{- end -}}
                {{- if $params.enableReadingTime | default true -}}
                    {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "clock") }}&nbsp;{{ T "readingTime" .ReadingTime }}&nbsp;
                {{- end -}}
                {{- $comment := .Scratch.Get "comment" | default dict -}}
                {{- /* Visitor Count */ -}}
                {{- if $comment.enable | and $comment.valine.enable | and $comment.valine.visitor -}}
                    <span id="{{ .RelPermalink }}" class="leancloud_visitors" data-flag-title="{{ .Title }}">
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "eye") }}&nbsp;<span class="leancloud-visitors-count"></span>&nbsp;{{ T "views" }}
                    </span>&nbsp;
                {{- end -}}
                {{- if $comment.enable | and $comment.waline.enable | and $comment.waline.pageview -}}
                    <span id="{{ .RelPermalink }}" class="leancloud_visitors" data-flag-title="{{ .Title }}">
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "eye") }}&nbsp;<span class="leancloud-visitors-count waline-pageview-count" data-path="{{ .RelPermalink }}"></span>&nbsp;{{ T "views" }}
                    </span>&nbsp;
                {{- end -}}
                {{- if $comment.enable | and $comment.twikoo.enable | and $comment.twikoo.visitor -}}
                    <span id="{{ .RelPermalink }}" class="leancloud_visitors" data-flag-title="{{ .Title }}">
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "eye") }}&nbsp;<span class="leancloud-visitors-count" id="twikoo_visitors"></span>&nbsp;{{ T "views" }}
                    </span>&nbsp;
                {{- end -}}
                {{- if $comment.enable | and $comment.artalk.enable | and $comment.artalk.pageview -}}
                    <span id="{{ .RelPermalink }}" class="leancloud_visitors" data-flag-title="{{ .Title }}">
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "eye") }}&nbsp;<span class="artalk-pv-count" id="ArtalkPV"></span>&nbsp;{{ T "views" }}
                    </span>&nbsp;
                 {{- end -}}

                {{- /* Comment Count */ -}}
                {{- if $comment.enable | and $comment.waline.enable | and $comment.waline.comment -}}
                    <span id="{{ .RelPermalink }}" class="comment_count" data-flag-title="{{ .Title }}">
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "comments") }}&nbsp;<span class="waline-comment-count" id="waline-comment-count" data-path="{{ .RelPermalink }}"></span>&nbsp;{{ T "comments" }}
                    </span>&nbsp;
                {{- end -}}
                {{- if $comment.enable | and $comment.twikoo.enable | and $comment.twikoo.commentCount -}}
                    <span id="{{ .RelPermalink }}" class="comment_count" data-flag-title="{{ .Title }}">
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "comments") }}&nbsp;<span class="twikoo-comment-count" id="twikoo-comment-count"></span>&nbsp;{{ T "comments" }}
                    </span>&nbsp;
                {{- end -}}
                {{- if $comment.enable | and $comment.artalk.enable | and $comment.artalk.commentCount -}}
                    <span id="{{ .RelPermalink }}" class="comment_count" data-flag-title="{{ .Title }}">
                        {{ partial "plugin/fontawesome.html" (dict "Style" "regular" "Icon" "comments") }}&nbsp;<span class="artalk-comment-count" id="ArtalkCount"></span>&nbsp;{{ T "comments" }}
                    </span>&nbsp;
                {{- end -}}
                {{- /* License */ -}}
                {{- $license := $params.license -}}
                {{- with $license -}}
                    <span>
                        {{ partial "plugin/fontawesome.html" (dict "Style" "solid" "Icon" "balance-scale") }}&nbsp;<span>{{- . | safeHTML -}}</span>&nbsp;
                    </span>&nbsp;
                {{- end -}}
            </div>
        </div>

        {{- /* Featured image */ -}}
        {{- $image := $params.featuredimage -}}
        {{- with $image -}}
            <div class="featured-image">
                {{- $optim := slice 
                    (dict "Process" "resize 800x webp q75" "descriptor" "800w")
                    (dict "Process" "resize 1200x webp q75" "descriptor" "1200w")
                    (dict "Process" "resize 1600x webp q75" "descriptor" "1600w") 
                -}}
                {{- dict "Src" . "Title" $.Description "Resources" $.Resources "Loading" "eager" "OptimConfig" $optim | partial "plugin/image.html" -}}
            </div>
        {{- end -}}      
        {{- /* Series list */ -}}
        {{- if $params.series | and $params.seriesNavigation -}}
                {{- range $key, $value := .Site.Taxonomies.series -}}
                    {{- range $params.series -}}
                        {{- if . | urlize | eq ($key | urlize) -}}
                            {{- $term := $.Site.GetPage "series" $key -}}
                            <div class="details series-nav open">
                                <div class="details-summary series-title">
                                    <span>{{ T "series" }} - {{ $term.Title }}</span>
                                    <span class="details-icon">{{ partial "plugin/fontawesome.html" (dict "Style" "solid" "Icon" "angle-right") }}</span>
                                </div>
                                <div class="details-content series-content">
                                    <nav>
                                        <ul>
                                            {{- range $value.Pages -}}
                                                {{- if .LinkTitle | eq $.Title -}}
                                                    <li><span class="active">{{ .LinkTitle }}</span></li>
                                                {{- else }}
                                                    <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
                                                {{- end }}
                                            {{- end -}}
                                        </ul>
                                    </nav>
                                </div>
                            </div>
                        {{- end -}}
                    {{- end -}}
                {{- end -}}
        {{- end -}}
        
        {{- /* Static TOC */ -}}
        {{- if $toc.enable -}}
            <div class="details toc print:!tw-block" id="toc-static"  kept="{{ if $toc.keepStatic }}true{{ end }}">
                <div class="details-summary toc-title">
                    <span>{{ T "contents" }}</span>
                    <span class="details-icon">{{ partial "plugin/fontawesome.html" (dict "Style" "solid" "Icon" "angle-right") }}</span>
                </div>
                <div class="details-content toc-content" id="toc-content-static">
                    {{- dict "Content" .TableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
                </div>
            </div>
        {{- end -}}

        {{- /* Content */ -}}
        <div class="content" id="content">
            {{- /* Outdated Article Reminder */ -}}
            {{- partial "single/outdatedArticleReminder.html" . -}}
            {{- dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
        </div>

        {{/* Related Content */}}
        {{- if $params.related.enable -}}
            {{- partial "related.html" . -}}
        {{- end -}}

        {{/* Sponsor */}}
        {{- partial "single/sponsor.html" . -}}

        {{- /* Footer */ -}}
        {{- partial "single/footer.html" . -}}

        {{- /* Comment */ -}}
        {{- partial "comment.html" . -}}
    </article>
{{- end -}}
